<template>
  <div class="content">
    <button-search placeholder="要搜索的角色" @search="searchEvent">
      <!-- 左边 -->
      <template #left>
        <el-button type="primary" size="mini" @click="isShowModel = true">添加角色</el-button>
      </template>
      <!-- 高级搜索表单 -->
      <template #form>
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="角色名称">
            <el-input style="width:160px" v-model="searchForm.name" placeholder="要搜索的角色" size="mini"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker size="mini" v-model="searchForm.create_time" type="daterange" 
            range-separator="至" start-placeholde="开始日期" 
            end-placeholde="结束日期" value-format="yyyy-MM-dd" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchEvent" size="mini">搜索</el-button>
            <el-button size="mini" @click="clearSearch">清空筛选条件</el-button>
          </el-form-item>
        </el-form>
      </template>
    </button-search>
    <!-- 表格 -->
    <el-table style="margin-top: 15px" :data="tableData" border>
      <el-table-column label="角色名称" prop="name" align="center"/>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time | dateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.update_time | dateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      style="margin-top: 20px"
      :current-page="page.current"
      :page-sizes="page.sizes"
      :page-size="page.size"
      layout="total,sizes,prev,pager,next,jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 添加/编辑 -->
    <el-dialog :title="userForm.id!=undefined?'修改角色':'创建角色'" :visible.sync="isShowModel" @close="closeModel" width="700px" :append-to-body="true">
      <el-form ref="ruleForm" :model="userForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="userForm.name" clearable style="width:300px" placeholder="请填写角色名称"></el-input>
        </el-form-item>
        <div class="menu-box">
          <div class="menu-box-label">角色菜单</div>
          <div class="menu-box-tree">
            <el-tree 
              :props="defaultProps"
              :data="menuList"
              ref="tree"
              node-key="id"
              show-checkbox
            ></el-tree>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowModel = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import buttonSearch from '@/components/ButtonSearch/index.vue'
import common from '@/mixins/common'
export default {
  mixins: [common],
  components: {
    buttonSearch
  },
  data(){
    return {
      urlEnum: 'ROLE_LIST', // 接口字典
      searchForm: {
        name: '',
        create_time: ''
      }, // 搜索表单
      userForm: {
        name: '',
        menu_ids: ''
      }, // 表单
      tableData: [], // 表格数据
      menuList: [], // 菜单列表
      defaultProps: {
        children: 'children',
        label: 'name'
      }, // 树配置选项
      rules: {
        name: [{required: true,message: '请输入角色名称',trigger: 'blur'}]
      }, // 表单验证
      isShowModel: false // 是否显示创建/删除弹窗
    }
  },
  created(){
    // 查询菜单列表
    this.axios.post('MENU_LIST').then(result => {
      this.menuList = result.data
    })
  },
  methods: {
    // 搜索
    searchEvent(e = false){
      // 简单搜索
      if(typeof e === 'string'){
        this.searchForm.name = e
      }
      // 高级搜索
      this.getList(true)
    },
    // 清空筛选条件
    clearSearch(){
      this.searchForm = {
        name: '',
        create_time: ''
      }
    },
    // 关闭弹窗
    closeModel(){
      this.userForm = {
        name: '',
        menu_ids: ''
      }
    },
    // 提交表单,创建或修改
    submitForm(){
      this.$refs.ruleForm.validate(e => {
        if(!e) return
        // 获取菜单勾选项
        let menu = this.$refs.tree.getCheckedNodes()
        let ids = ''
        menu.forEach(v => {
          ids += `,${v.id}`
        })
        this.userForm.menu_ids = ids.substr(1)
        this.axios.post(this.userForm.id?'EDIT_ROLE':'CREATE_ROLE',this.userForm).then(() => {
          // 成功提示
          this.$message({
            type: 'success',
            message: this.userForm.id ? '修改成功' : '创建成功'
          })
          this.isShowModel = false
          this.getList()
        })
      })
    },
    // 打开编辑弹窗
    handleEdit(item){
      this.userForm = {
        id: item.id,
        name: item.name,
        menu_ids: item.menu_ids
      }
      // 选中菜单
      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys(item.menu_ids.split(',') || [])
      })
      this.isShowModel = true
    },
    // 删除角色
    handleDelete(id){
      this.$confirm('您确定要删除该角色？','提示',{
        confirmButtonText: '确定',
        canceButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.post('DELETE_ROLE',{id}).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.content{
  padding: 15px 10px;
}
.avatar{
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.menu-box{
  display: flex;
  .menu-box-label{
    font-size: 14px;
    color: #606266;
    margin-left: 10px;
    font-weight: 700;
  }
  .menu-box-tree{
    width: 300px;
    padding: 8px 0;
    margin-left: 13px;
    border: 1px solid #eee;
  }
}
</style>